<template>
  <page-card title="字典管理">
    <div class="table-page-search-wrapper">
      <a-row type="flex"
             style="margin-bottom:20px"
             justify="end">
        <a-col v-if="hasPermission('add')"
               flex="1">
          <a-button type="primary"
                    icon="plus"
                    action
                    @click="handleEdit()">
            新增
          </a-button>
        </a-col>

        <template>
          <a-col>
            <a-input-search v-model="queryParam.name"
                            placeholder="请输入字典名称，归类"
                            @search="search"
                            allowClear />
          </a-col>
        </template>
      </a-row>
    </div>
    <a-table :columns="columns"
             :rowKey="(record, index) => index"
             :data-source="data"
             @change="handleTableChange"
             :pagination="pagination">
      <span slot="status"
            :style="{color:statusCell[record.status].color}"
            slot-scope="text, record">{{ statusCell[record.status].text }}</span>
      <span slot="action"
            slot-scope="text, record">
        <template v-if="hasPermission('edit')">
          <a @click="handleEdit(record.id)">编辑</a>
        </template>
        <a-divider v-if="hasPermission('edit', 'delete')"
                   type="vertical" />
        <template v-if="hasPermission('delete')">
          <a-popconfirm title="确定要删除吗？"
                        ok-text="是"
                        cancel-text="否"
                        @confirm="handleDelete(record.id)">
            <a href="javascript:;">删除</a>
          </a-popconfirm>
        </template>
      </span>
    </a-table>
  </page-card>
</template>

<script>
// import { del, page } from '../..//api/dictionary'
import { del, page } from '../../api/dictionary/index'
import { PAGINATION, TABLE_STATUS_CELL } from '../../constants'

export default {
  data () {
    return {
      pagination: PAGINATION,
      statusCell: TABLE_STATUS_CELL,
      queryParam: {},
      data: [],
      columns: [
        // {
        //   title: '归类名称',
        //   width: 200,
        //   align: 'center',
        //   dataIndex: 'name',
        //   ellipsis: true
        // },
        {
          title: '字典名称',
          width: 200,
          align: 'center',
          ellipsis: true,
          dataIndex: 'dictValue'
        },
        {
          title: '字典值',
          width: 200,
          align: 'center',
          ellipsis: true,
          dataIndex: 'dictKey'
        },
        {
          title: '排序号',
          dataIndex: 'orderNo',
          align: 'center',
          width: 150
        },
        {
          title: '描述',
          ellipsis: true,
          dataIndex: 'remark'
        },
        {
          title: '创建时间',
          align: 'center',
          width: 200,
          dataIndex: 'createTime',
          customRender: (v) => (v ? v.substring(0, 16) : '')
        }
      ]
    }
  },
  created () {
    if (this.hasPermissions('edit', 'delete')) {
      this.columns.push({
        title: '操作',
        width: 130,
        align: 'center',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  activated () {
    this.load()
  },
  methods: {
    search () {
      this.pagination.current = 1
      this.load()
    },
    load () {
      this.queryParam.currPage = this.pagination.current
      this.queryParam.pageSize = this.pagination.pageSize
      page({ ...this.queryParam }).then((response) => {
        this.data = response.list
        this.pagination.current = response.currPage
        this.pagination.total = response.totalCount
        this.pagination.pageSize = response.pageSize
      })
    },
    reload () {
      this.load({
        currPage: this.pagination.current,
        pageSize: this.pagination.pageSize,
        queryParam: this.queryParam
      })
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = pagination
      this.load()
    },
    handleEdit (id) {
      this.$router.push({
        path: '/dictionary/edit',
        query: { id }
      })
    },
    handleDelete (id) {
      del({ id: id }).then((res) => {
        // this.message.info('字典管理删除成功')
        this.load()
      })
    }
  }
}
</script>

<style>
</style>
